extends ../layout

block content
  style.
    .hidden{display:none;}
  .container-lg
    .row
      .col-md-6
        form#form.form-horizontal(method="post", action="/admin/article")
          if article._id
            input(type="hidden", name="_id", value=article._id)
          .form-group
            label.col-sm-2.control-label(for="inputCategory") 文章分类
            .col-sm-10
              input#inputCategory.form-control(type="text", name="categoryName", value=article.categoryName)
          .form-group
            label.col-sm-2.control-label 分类选择
            .col-sm-10
              select.form-control(name="category")
                for cat in categories
                    option(value=cat._id,selected=article.category==cat._id) #{cat.name}
          .form-group
            label.col-sm-2.control-label(for="inputTitle") 文章标题
            .col-sm-10
              input#inputTitle.form-control(type="text", name="title", value=article.title)
          .form-group
              label.col-sm-2.control-label(for="inputText") 文章正文
              .col-sm-10
                  textarea#inputText.form-control(name="text")
                  .textbox.hidden #{article.text}
          .form-group
            label.col-sm-2.control-label(for="inputSummary") 文章摘要
            .col-sm-10
              textarea#inputSummary.form-control(type="text", name="abstract")= article.abstract
          .form-group
            label.col-sm-2.control-label(for="inputSourceurl") 原文地址
            .col-sm-10
              textarea#inputSourceurl.form-control(type="text", name="sourceurl")= article.sourceurl
          //- .form-group
          //-     label.col-sm-2.control-label(for="inputPoster") 图文图
          //-     .col-sm-10
          //-         input#inputPoster.form-control(type="text",name="poster",value=article.poster,readonly=true)
          //-         input#detailImgInput.form-control(type="file") 
          //-         a.btn.btn-default#upload.pull-right 上传
          .form-group
              label.col-sm-2.control-label(for="inputPoster") 图文图
              .col-sm-10
                  input#inputPoster.form-control(type="text",name="poster",value=article.poster)
                  input#detailImgInput.form-control(type="file") 
                  a.btn.btn-default#upload.pull-right 上传
          .form-group
              .col-sm-2
              .col-sm-4
                button.btn.btn-block.submit.btn-primary(type="submit") 录入
              .col-sm-4
                button.btn.btn-primary#preview 预览
      .col-md-6
        .phone-gap
          .text-gap
  
  script(src="/kindeditor/kindeditor-min.js")
  script(src="/kindeditor/zh_CN.js")
  script.
    $(function() {
      var img,editor;
      $("#detailImgInput").change(function(event){
          img = event.target.files[0];
          //验证图片格式
          if(!(img.type.indexOf('image')==0 && img.type && /\.(?:jpg|JPG|png|PNG|gif|GIF|jpeg|JPEG)$/.test(img.name)) ){
              alert('图片格式只能是jpg,gif,png,jpeg');
              $(this).val('');
              return;
          }
      });

      $("#upload").on('click',function(){
        if(!img){
            return ;
        }
        var data = new FormData();
        data.append("file", img);
        $.ajax({
          type: 'post',
          url: config.api+'admin/upload?provider=aliyun',
          contentType: false,
          processData: false,
          data: data,
          success: function(result){
              if(result.url){
                  $("#inputPoster").val(result.url)
              }
          }
        });
      });

      $(".submit").on('click',function(e){
        e.preventDefault();
        $("#inputText").val(editor.html());
        var data = {
            _id: '#{article._id}',
            categoryName: $("#inputCategory").val(),
            category: $("select[name=category]").val(),
            title: $("#inputTitle").val(),
            text: editor.html(),
            abstract: $("#inputSummary").val(),
            sourceurl: $("#inputSourceurl").val(),
            poster: $("#inputPoster").val()
        };
        if(!data._id){
            delete data._id;
        }
        $.ajax({
          type: 'post',
          url: $("#form").attr('action'),
          data: data,
          success: function(result){
              if(result.success){
                 location.href="/admin/article/one/"+result.id;
              }
          }
        }); 
      });

      $("#preview").on('click',function(e){
          e.preventDefault();
          var htmls = editor.html();
          $(".text-gap").html(htmls);
      });

      KindEditor.ready(function(K) {
        editor = K.create('textarea#inputText', {
            uploadJson: config.api+'admin/upload?provider=aliyun',
            allowFileManager : true,
            height: '400px'
        });
        setTimeout(function(){
            editor.insertHtml($(".textbox").text());
        },500)
        
      });
    })